package tr.edu.metu.is.mega.client.schedule;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import tr.edu.metu.is.mega.common.dto.ActivityDTO;
import tr.edu.metu.is.mega.common.dto.ActivityTemplateDTO;

/**
 * Class to construct activities from template
 */
public final class ScheduleTemplateTransformHelper {

    public static final long HOUR_IN_MILLIS = 3600000;

    public static final long DAY_IN_MILLIS = 24 * HOUR_IN_MILLIS;

    /**
     * Hidden constructor.
     */
    private ScheduleTemplateTransformHelper() {
    }

    /**
     * Transforms activity template to activity. Semester start date is used to
     * find activity dates from template.
     * 
     * @param templates list of templates
     * @param semesterStartDate first day of semester
     * @return list of transformed activities
     */
    public static List<ActivityDTO> transform(List<ActivityTemplateDTO> templates, Date semesterStartDate) {

        List<ActivityDTO> activityList = new ArrayList<ActivityDTO>(templates.size());

        for (ActivityTemplateDTO activityTemplate : templates) {
            ActivityDTO activity = new ActivityDTO();

            activity.setDescription(activityTemplate.getDescription());
            activity.setType(activityTemplate.getSubmissionType());

            long time = semesterStartDate.getTime() + activityTemplate.getDueDate().intValue() * DAY_IN_MILLIS
                    + activityTemplate.getDueTime().intValue() * HOUR_IN_MILLIS;

            activity.setDueDate(new Date(time));
            if (activityTemplate.getAlertTime() != null) {
                activity.setAlertTime(new Date(time + activityTemplate.getAlertTime().intValue() * HOUR_IN_MILLIS));
            }
            activityList.add(activity);
        }
        return activityList;
    }
}
